﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;
using XCommunity.Web;

public partial class Login : System.Web.UI.Page
{
  protected void Page_Load( object sender, EventArgs e )
  {


    AccountManage.Logout();

    if ( Request.QueryString["logout"] != null )
    {
      Response.Write( string.Format( "<script type=\"text/javascript\" language=\"javascript\">window.location = \"{0}\"</script>", Request.QueryString["source"] ) );
      return;
    }



    string validateCode = Request.Form["ValidateCode"];

    if ( validateCode != null )
    {
      string sessionValidateCode = (string) Session["ValidateCode"];
      Session.Remove( "ValidateCode" );

      if ( !string.Equals( validateCode, sessionValidateCode, StringComparison.OrdinalIgnoreCase ) )
        ShowMessage( "验证码错误，请点击返回继续" );


      string username = Request.Form["Username"];
      string password = Request.Form["Password"];

      if ( string.IsNullOrEmpty( username ) || string.IsNullOrEmpty( password ) )
        ShowMessage( "必须填写用户名和密码，请点击返回继续" );


      LoggedAccount account = AccountManage.Login( username, password );

      if ( account != null )
        Response.Write( string.Format( "<script type=\"text/javascript\" language=\"javascript\">window.location = \"{0}\"</script>", Request.QueryString["source"] ) );
      else
        Response.Write( "登录失败，请点击返回继续" );

    }
  }

  private void ShowMessage( string message )
  {
    Response.Write( message );
    Response.End();
  }

}